import 'package:boss/generated/i18n.dart';
import 'package:boss/provider/account_provider.dart';
import 'package:boss/provider/binding_provider.dart';
import 'package:boss/provider/business_setting_provider.dart';
import 'package:boss/provider/cloud_printer_provider.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/provider/home_provider.dart';
import 'package:boss/provider/login_user_provider.dart';
import 'package:boss/provider/material_provider.dart';
import 'package:boss/provider/necessary_order_provider.dart';
import 'package:boss/provider/offers_provider.dart';
import 'package:boss/provider/package_group_provider.dart';
import 'package:boss/provider/regist_provider.dart';
import 'package:boss/provider/staff_provider.dart';
import 'package:boss/provider/store_message_provider.dart';
import 'package:boss/provider/table_provider.dart';
import 'package:boss/provider/takeout_process_provider.dart';
import 'package:boss/provider/version_provider.dart';
import 'package:boss/route/navigator_manager.dart';
import 'package:boss/router.dart';
import 'package:boss/theme/theme.dart';
import 'package:boss/ui/page/commission/provider/commission_provider.dart';
import 'package:boss/ui/page/marketing/coupon/couponList/provider/coupon_list_provider.dart';
import 'package:boss/ui/page/role/provider/role_provider.dart';
import 'package:boss/ui/page/unit/unit_provider.dart';
import 'package:boss/util/boss_utils.dart';
import 'package:boss/util/version_util.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';

import 'page/role/page/roleDetail/provider/role_detail_provider.dart';
import 'widget/loader/pull_refresh_config.dart';

class GlobalProviderApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (ctx) => CommodityProvider()),
        ChangeNotifierProvider(create: (ctx) => MaterialProvider()),
        ChangeNotifierProvider(create: (ctx) => TableProvider()),
        ChangeNotifierProvider(create: (ctx) => LoginUserProvider()),
        ChangeNotifierProvider(create: (ctx) => RegistProvider()),
        ChangeNotifierProvider(create: (ctx) => NecessaryOrderProvider()),
        ChangeNotifierProvider(create: (ctx) => OffersProvider()),
        ChangeNotifierProvider(create: (ctx) => StoreMessageProvider()),
        ChangeNotifierProvider(create: (ctx) => BindingProvider()),
        ChangeNotifierProvider(create: (ctx) => HomeProvider()),
        ChangeNotifierProvider(create: (ctx) => AccountProvider()),
        ChangeNotifierProvider(create: (ctx) => StaffProvider()),
        ChangeNotifierProvider(create: (ctx) => VersionProvider()),
        ChangeNotifierProvider(create: (ctx) => PackageGroupProvider()),
        ChangeNotifierProvider(create: (ctx) => TakeoutProcessProvider()),
        ChangeNotifierProvider(create: (ctx) => BusinessSettingProvider()),
        ChangeNotifierProvider(create: (ctx) => CloudPrinterProvider()),
        ChangeNotifierProvider(create: (ctx) => CommissionProvider()),
        ChangeNotifierProvider(create: (ctx) => RoleProvider()),
        ChangeNotifierProvider(create: (ctx) => RoleDetailProvider()),
        ChangeNotifierProvider(create: (ctx) => CouponListProvider()),
        ChangeNotifierProvider(create: (ctx) => UnitProvider()),
      ],
      child: App(),
    );
  }
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    super.initState();

    Future.delayed(Duration(seconds: 5), () {
      VersionUtil.checkVersion(GlobalPageContext.instance.context, isNeedToast: false);
    });
  }

  @override
  Widget build(BuildContext context) {
    return BotToastInit(
      child: PullRefreshConfig(
        child: MaterialApp(
          title: '学习强店老板端',
          routes: router,
          theme: lightTheme,
          debugShowCheckedModeBanner: false,
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            S.delegate,
          ],
          supportedLocales: S.delegate.supportedLocales,
          locale: Locale('zh', 'CN'),
          navigatorObservers: [
            BotToastNavigatorObserver(),
            NavigatorManager.instance,
            AnalyticsUtil.instance.observer,
          ],
          builder: (context, child) {
            return GestureDetector(
              onTap: () => BossUtils.tryLostFocus(context),
              child: child,
            );
          },
        ),
      ),
    );
  }
}
